trigger AssignmentGroup_CycleTeamAssignment on Assignment_Groups__c (after insert, after update)
{
    list<User> userUpdates = new list<User>();
    Map<String, String> userMap = new Map<String, String>();
    Map<String, Assignment_Groups__c> agMap = new Map<String, Assignment_Groups__c>();
    list<String> userIds = new list<String>();
    Assignment_Groups__c ag = new Assignment_Groups__c();
    try
    {  
        for (Assignment_Groups__c assignment : Trigger.new)
        {
            agMap.put(assignment.User__c, assignment);
            userIds.add(assignment.User__c);
        }
        
        List<User> groupMembers = [select Id, Cycle_Team__c from User where Id IN :userIds ];
        for(User u : groupMembers)
        {
            ag = agMap.get(u.Id); 
            if (u.Cycle_Team__c != ag.Cycle_Team__c)
            {
              // last update wins resolution semantics
              userMap.put(ag.User__c, ag.Cycle_Team__c);
            }
        }
        
        Set<String> keys = userMap.keySet();
        for (String userId : keys)
        {
            User groupMember = new User(Id=userId); 
            groupMember.Cycle_Team__c = userMap.get(userId);
            userUpdates.add(groupMember);
        }
        try
        {
            update userUpdates;
        }
        catch (Exception e)
        {
          String emsg = 'Cycle Team assignment exception: ' + e.getMessage();
          System.debug(Logginglevel.ERROR, emsg);
          throw e;
        }      
    }
    catch (Exception e)
    {
        String emsg = 'Assignment Group trigger exception: ' + e.getMessage();
        System.debug(Logginglevel.ERROR, emsg);
        throw e;
    }
}